<?php
namespace app\admin\controller;

class Menu extends Base
{

    /**
     * 菜单列表
     * @return html
     */
    public function index()
    {
        $where        = [];
        $title        = trim( input( 'title' ) );
        $pid          = input( 'pid' ) ? input( 'pid' ) : 0;
        $where['pid'] = $pid;
        $title && $where['title'] = ['like', '%'.$title.'%'];
        $field  = 'id, title, pid, sort, url, group, status';
        $list   = db( 'menu' )->field( $field )->where( $where )->select();
        $pTitle = '根菜单';
        if( $list ) {
            if( $pid ) {
                $pTitle = db( 'menu' )->where( ['id'=>$pid] )->value( 'title' );
            }
        }

        $this->assign( 'list', $list );
        $this->assign( 'pid', ['pid'=>$pid] );
        $this->assign( 'pTitle', $pTitle );
        return $this->fetch();
    }

    /**
     * 添加菜单
     * @return html
     */
    public function add()
    {
        $menu = model( 'Menu' );
        $pid  = input( 'pid' ) ? input( 'pid' ) : 0;
        if( request()->isPost() ) {
			
            $data  = input();
			
			$valid 		 = new \app\admin\validate\Menu;
            if( !$valid->check( $data ) ) {
                $this->error( $valid->getError() );
            } else {
                $data['icon'] = isset( $data['icon'] ) && $data['icon'] ? $data['icon'] : 'fa fa-list';
                if( $menu->addMenu( $data ) ) {
                    //操作日志
                    add_system_log( $this->uid, 1, 1, '添加系统菜单' );
                    $this->success( '添加成功', url( 'menu/index', array( 'pid'=>$pid ) ) );
                } else {
                    $this->error( '添加失败' );
                }
            }
        } else {
            //获取所有菜单
            $where           = [];
            $where['status'] = 1;
            $tree  = db( 'menu' )->where( $where )->select();

            $menus = $menu->toFormatTree( $tree );
            $menus = array_merge( [0=>['id'=>0, 'title_show'=>'顶级菜单']], $menus );
            $this->assign( 'Menus', $menus );
            $this->assign( 'pid', $pid );
            return $this->fetch();
        }
    }

    /**
     * 编辑菜单
     * @return html
     */
    public function edit()
    {
        $menu = model( 'Menu' );
        if( request()->isPost() ) {
            $data  = input();
            $valid 		 = new \app\admin\validate\Menu;
            if( !$valid->check( $data ) ) {
                $this->error( $valid->getError() );
            } else {
                $data['icon'] = isset( $data['icon'] ) && $data['icon'] ? $data['icon'] : 'fa fa-list';
                $bool = $menu->editMenu( $data );
                if( $bool !== false ) {
                    //操作日志
                    add_system_log( $this->uid, 1, 2, '编辑系统菜单' );
                    $this->success( '编辑成功', url( 'menu/index', ['pid'=>$data['pid']] ) );
                } else {
                    $this->error( '编辑失败' );
                }
            }
        } else {
            $id    = input( 'id' );
            $find  = $menu->getFindOne( $id );
            //获取所有菜单
            $where           = [];
            $where['status'] = 1;
            $tree  = db( 'menu' )->where( $where )->select();
            $menus = $menu->toFormatTree( $tree );
            $menus = array_merge( [0=>['id'=>0, 'title_show'=>'顶级菜单']], $menus );
            $this->assign( 'Menus', $menus );
            $this->assign( 'find', $find );
            return $this->fetch();
        }
    }

    /**
     * 启用|禁用
     * @return html
     */
    public function state()
    {
        $id     = input( 'id' );

        $status = input( 'status' );
        if( $id && $status ) {
            $map = [
                'id' => ['in',$id],
            ];
            $bool = model( 'Menu' )->stateMenu( $map, ['status'=>$status] );
            if( $bool !== false ) {
                //操作日志
                $acname = $status == 1 ? '启用' : '禁用';
                add_system_log( $this->uid, 1, 2, $acname.'系统菜单' );
                $this->success( '设置成功' );
            } else {
                $this->error( '设置失败' );
            }
        } else {
            $this->error( '参数有误' );
        }
    }

    /**
     * 删除菜单
     * @return html
     */
    public function remove()
    {
        $data = input( 'id' );
		
        if( $data ) {
            $auth_member = model( 'Menu' );
            if( $auth_member->removeMenu( $data ) ) {
                //操作日志
                add_system_log( $this->uid, 1, 4, '删除系统菜单' );
                $this->success( '删除成功' );
            } else {
                $this->error( '删除失败，可能是该菜单下包含子菜单' );
            }
        } else {
            $this->error( '提交的数据有误' );
        }
    }

}